package com.example.li.day9_19_work_guajiang;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class GuaJiangView extends View {

    private Paint textPaint;
    private int text_color;
    private int text_size;
    private Paint eraserPaint;
    private Bitmap bitmap;
    private float x;
    private float y;
    private Canvas mcanvas;

    public GuaJiangView(Context context) {
        super(context);
    }

    public GuaJiangView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        into(attrs);
        intoTextPaint();
    }

    private void intoCanvas() {
        bitmap = Bitmap.createBitmap(widthView, heightView, Bitmap.Config.ARGB_8888);
        mcanvas = new Canvas(bitmap);
        mcanvas.drawColor(Color.rgb(50,15,50));
    }

    private void intoTextPaint() {
        textPaint = new Paint();
        textPaint.setColor(text_color);
        textPaint.setTextSize(text_size);
        textPaint.setTextAlign(Paint.Align.CENTER);

        eraserPaint = new Paint();
        eraserPaint.setStrokeWidth(10);
        eraserPaint.setStrokeCap(Paint.Cap.ROUND);
        eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }

    private void into(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.GuaJiangView);
        text_color = typedArray.getColor(R.styleable.GuaJiangView_text_color, Color.BLUE);
        text_size = typedArray.getInteger(R.styleable.GuaJiangView_text_size, 30);
        typedArray.recycle();
    }

    int widthView;
    int heightView;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        switch (modeWidth) {
            case MeasureSpec.EXACTLY:
                widthView = sizeWidth;
                break;
            case MeasureSpec.AT_MOST:
                widthView = 300;
                break;
        }
        switch (modeHeight) {
            case MeasureSpec.EXACTLY:
                heightView = sizeHeight;
                break;
            case MeasureSpec.AT_MOST:
                heightView = 300;
                break;
        }
        setMeasuredDimension(widthView, heightView);
        intoCanvas();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        String text = "dddddddd";
        canvas.drawText(text, widthView / 2, heightView / 2, textPaint);
        canvas.drawBitmap(bitmap,0,0,null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float lastX= x;
        float lastY= y;
        x = event.getX();
        y = event.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                mcanvas.drawLine(lastX,lastY,x,y,eraserPaint);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                invalidate();
                break;
        }
        return true;
    }
}
